package com.woniuxy.configuration;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

public class DruidConfiguration {
	@Value("${spring.datasource.url}")
	private String url;
	@Value("${spring.datasource.username}")
	private String username;
	@Value("${spring.datasource.password}")
	private String password;
	@Value("${spring.datasource.driver-class-name}")
	private String driverClassName;
	@Value("${spring.druid.initialSize}")
	private int initialSize;
	@Value("${spring.druid.minIdle}")
	private int minIdle;
	@Value("${spring.druid.maxActive}")
	private int maxActive;
	@Value("${spring.druid.maxWait}")
	private int maxWait;
	@Value("${spring.druid.timeBetweenEvictionRunsMillis}")
	private int timeBetweenEvictionRunsMillis;
	@Value("${spring.druid.minEvictableIdleTimeMillis}")
	private int minEvictableIdleTimeMillis;
	@Value("${spring.druid.validationQuery}")
	private String validationQuery;
	@Value("${spring.druid.testWhileIdle}")
	private boolean testWhileIdle;
	@Value("${spring.druid.testOnBorrow}")
	private boolean testOnBorrow;
	@Value("${spring.druid.testOnReturn}")
	private boolean testOnReturn;
	@Value("${spring.druid.poolPreparedStatements}")
	private boolean poolPreparedStatements;
	@Value("${spring.druid.maxPoolPreparedStatementPerConnectionSize}")
	private int maxPoolPreparedStatementPerConnectionSize;
	@Value("${spring.druid.filters}")
	private String filters;
	@Value("{spring.druid.connectionProperties}")
	private String connectionProperties;

	// 创建dataSource
	@Bean
	@Primary
	public DataSource dataSource() {
		DruidDataSource datasource = new DruidDataSource();
		datasource.setUrl(url);
		datasource.setUsername(username);
		datasource.setPassword(password); // 这里可以做加密处理
		datasource.setDriverClassName(driverClassName);
		// configuration
		datasource.setInitialSize(initialSize);
		datasource.setMinIdle(minIdle);
		datasource.setMaxActive(maxActive);
		datasource.setMaxWait(maxWait);
		datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
		datasource.setValidationQuery(validationQuery);
		datasource.setTestWhileIdle(testWhileIdle);
		datasource.setTestOnBorrow(testOnBorrow);
		datasource.setTestOnReturn(testOnReturn);
		datasource.setPoolPreparedStatements(poolPreparedStatements);
		datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
		try {
			datasource.setFilters(filters);
		} catch (SQLException e) {
		}
		datasource.setConnectionProperties(connectionProperties);
		return datasource;
	}

	// 创建Servlet 专门用来监控数据的servlet
	@Bean
	public ServletRegistrationBean<StatViewServlet> statViewServlet() {
		ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<StatViewServlet>(
				new StatViewServlet(), "/druid/*");
		// 设置ip白名单，指定哪个电脑能够对后台数据进行监控
		servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
		// servletRegistrationBean.addInitParameter("deny","192.168.0.102");//设置ip黑名单，优先级高于白名单
		// 设置控制台管理用户
		servletRegistrationBean.addInitParameter("loginUsername", "root");
		servletRegistrationBean.addInitParameter("loginPassword", "root");
		// 是否可以重置数据
		servletRegistrationBean.addInitParameter("resetEnable", "false");
		return servletRegistrationBean;
	}
	
	// 创建过滤器
	@Bean
	public FilterRegistrationBean<WebStatFilter> statFilter(){
	//创建过滤器
	FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new
	FilterRegistrationBean<WebStatFilter>(new WebStatFilter());
	//设置过滤器过滤路径监控所有的请求
	filterRegistrationBean.addUrlPatterns("/*");
	//忽略过滤的形式
	filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
	return filterRegistrationBean;
	}
}
